package com.kunpeng.jvsichuanairspider.bll;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Administrator on 2017/5/10.
 */
public class CallInterfaceHelper {
    private static Logger LOGGER = LoggerFactory.getLogger(CallInterfaceHelper.class);


    public String getMethod(String urlStr) {
        String responseMessage = "";
        HttpURLConnection httpURLConnection = null;
        BufferedReader bufferedReader = null;
        StringBuffer stringBuffer = new StringBuffer();
        try {
            URL urlPost = new URL(urlStr);
            httpURLConnection = (HttpURLConnection) urlPost.openConnection();
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            LOGGER.debug("打开连接:" + urlStr);
            httpURLConnection.connect();
            InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader);
            while ((responseMessage = bufferedReader.readLine()) != null) {
                stringBuffer.append(responseMessage);
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            LOGGER.error("连接失败，url = {}", urlStr);
            e.printStackTrace();
            return "failed";
        } finally {
            try {
                if (null != bufferedReader) {
                    bufferedReader.close();
                }
                if (null != httpURLConnection) {
                    httpURLConnection.disconnect();
                }
            } catch (Exception e2) {
                LOGGER.error("http connection close error:{}", e2);
            }
        }
    }
    public String postMethod(String urlStr, String postData)
    {
        String responseMessage = "";
        StringBuffer bufferResponse = new StringBuffer();
        HttpURLConnection httpURLConnection = null;
        DataOutputStream out = null;
        BufferedReader bufferedReader = null;
        try {
            URL urlPost = new URL(urlStr);
            httpURLConnection = (HttpURLConnection) urlPost.openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            LOGGER.debug("打开连接:" + urlStr);
            httpURLConnection.connect();
            out = new DataOutputStream(httpURLConnection.getOutputStream());
            out.writeBytes(postData);
            //如果有缓冲的，你需要调flush()
            out.flush();
            bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            while ((responseMessage = bufferedReader.readLine()) != null){
                bufferResponse.append(responseMessage);
            }
            return bufferResponse.toString();

        } catch (Exception e) {
            LOGGER.error("连接失败，url = {}", urlStr);
            e.printStackTrace();
            return "failed";
        } finally {
            try{
                if(null != out){
                    out.close();
                }
                if(null != bufferedReader){
                    bufferedReader.close();
                }
                if(null != httpURLConnection){
                    httpURLConnection.disconnect();
                }
            }catch (Exception e2){
                LOGGER.error("http connection close error:{}", e2);
            }

        }
    }

}
